﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Common;
using Common.Model;
namespace BLL
{
    //Customer
    public partial class CustomerBLL
    {

        private readonly DAL.CustomerDAL dal = new DAL.CustomerDAL();
        public CustomerBLL()
        { }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }
        /// <summary>
        ///根据用户名， 是否存在该记录
        /// </summary>
        public bool Exists(String Name)
        {
            return dal.Exists(Name);
        }
        /// <summary>
        /// 判断是否认证
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Authenticated(int Id)
        {
            return dal.Authenticated(Id);
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public int Login(String Name, String Password)
        {
            return dal.Login(Name, Password);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Common.Model.Customer model)
        {
            return dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Common.Model.Customer model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除几条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Common.Model.Customer GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Common.Model.Customer GetModelByCache(int Id)
        {

            string CacheKey = "CustomerModel-" + Id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Common.Model.Customer)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Common.Model.Customer> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表,DataTable转换为对象列表
        /// </summary>
        public List<Common.Model.Customer> DataTableToList(DataTable dt)
        {
            List<Common.Model.Customer> modelList = new List<Common.Model.Customer>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Common.Model.Customer model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Common.Model.Customer();
                    if (dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    model.name = dt.Rows[n]["name"].ToString();
                    model.Password = dt.Rows[n]["Password"].ToString();
                    model.Nick = dt.Rows[n]["Nick"].ToString();
                    if (dt.Rows[n]["CreateTime"].ToString() != "")
                    {
                        model.CreateTime = DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
                    }
                    model.Email = dt.Rows[n]["Email"].ToString();
                    model.CellPhone = dt.Rows[n]["CellPhone"].ToString();
                    if (dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }


                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得所有数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion

    }
}